<template>
  <div class="app-container"
       v-loading.fullscreen.lock="allLoading"
       element-loading-text="拼命处理中,请耐心等待"
       element-loading-spinner="el-icon-loading"
       element-loading-background="rgba(0, 0, 0, 0.8)"
  >
    <el-dialog :close-on-click-modal="changeBitFlagIsNone" :title="mainDialogInfo.title" :visible.sync="showDialog"
               width="500px" append-to-body @close="closeDialog">
      <el-form ref="form" :disabled="changeBitFlagIsNone" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-form-item prop="model_name" label="模型ID">
            <el-input v-model="form.model_name" placeholder="请输入" />
          </el-form-item>
          <el-form-item prop="model_group" label="分组">
            <el-input v-model="form.model_group" rows="2"  type="textarea" placeholder="请输入" />
          </el-form-item>
          <el-form-item prop="description" label="描述">
            <el-input v-model="form.description" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <el-select v-model="form.type" placeholder="请选择">
              <el-option
                v-for="item in dicts.aiModelTypeOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="支持工具" prop="status">
            <el-switch
              v-model="form.has_tool"
              active-color="#ff4949"
              inactive-color="#13ce66"
              active-value="NO"
              inactive-value="YES"
              active-text="否"
              inactive-text="是">
            </el-switch>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-switch
              v-model="form.status"
              active-color="#ff4949"
              inactive-color="#13ce66"
              active-value="disabled"
              inactive-value="enabled"
              active-text="禁用"
              inactive-text="启用">
            </el-switch>
          </el-form-item>
          <template v-if="sksUtils.numberIsNotZeroOrNull(form.id)">
            <el-form-item prop="created_at" label="创建时间">
              <el-date-picker v-model="form.created_at" type="datetime" placeholder="选择创建时间"  readonly/>
            </el-form-item>
            <el-form-item prop="updated_at" label="更新时间">
              <el-date-picker v-model="form.updated_at" type="datetime" placeholder="选择更新时间"  readonly/>
            </el-form-item>
          </template>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <template v-if="!changeBitFlagIsNone">
          <el-button type="primary" @click="submitMainDialog">确 定</el-button>
        </template>
        <el-button @click="cancelMainDialog">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { sksDialogFormMixin } from 'sks-plugin-el-erp/lib/dialog-form'
import { addAIModel, getAIModel, updateAIModel } from '@/api/ai'
import { numberIsNotZeroOrNull } from 'sks-plugin-el-erp/lib/core'

export default {
  name: "AddAIModelDialog",
  components: {
  },
  mixins: [
    sksDialogFormMixin,
  ],
  data() {
    return {
      showEmojiDialog: false,
      defaultForm: {
        id: undefined,
        ai_api_id: undefined,
        model_name: undefined,
        model_group: undefined,
        description: undefined,
        type: undefined,
        has_tool: undefined,
        status: undefined,
      },
      defaultFormNotNull: {
        virtualDocumentList: [],
      },

      rules: {
        model_name: [
          {required: true, message: '不能为空', trigger: "blur"},
        ],
        model_group: [
          {required: true, message: '不能为空', trigger: "blur"},
        ],
        description: [
          {required: true, message: '不能为空', trigger: "blur"},
        ],
        status: [
          {required: true, message: '不能为空', trigger: "blur"},
        ],
      },
    };
  },
  methods:{
    numberIsNotZeroOrNull,
    showByUniqueId({uniqueId,extra,dialogInfo,virtualChangeBitFlag=this.sksUtils.CHANGE_BIT_FLAG.none}){
      getAIModel({id:uniqueId}).then(res=>{
        console.log(res)
        let data=res.data
        this.show({
          extra: extra,
          form: this.sksUtils.mergeFromObject({},data,{
            virtualChangeBitFlag: virtualChangeBitFlag,
          }),
          dialogInfo: dialogInfo
        })
      })
    },
    validateSuccess() {
      let data = this.sksUtils.mergeFromObject({}, this.form);

      if (!this.sksUtils.isNullOrUndefined(this.mainDialogInfo.submitCallback)) {
        this.mainDialogInfo.submitCallback({submitData: data, ref: this})
        //this.showDialog=false
        return
      }
      switch(data.virtualChangeBitFlag) {
        case this.sksUtils.CHANGE_BIT_FLAG.add:
          //此处换成新增接口 start
          addAIModel(data).then(res=>{
            this.sksUtils.msgSuccess("新增成功");
            this.closeDialog()
            if (!this.sksUtils.isNullOrUndefined(this.mainDialogInfo.submitCallbackSuccess)){
              this.mainDialogInfo.submitCallbackSuccess({resData: res.data})
            }
          })
          //此处换成新增接口 end
          break;
        case this.sksUtils.CHANGE_BIT_FLAG.modify:
          updateAIModel(data).then(res=>{
            this.sksUtils.msgSuccess("修改成功");
            this.closeDialog()
            if (!this.sksUtils.isNullOrUndefined(this.mainDialogInfo.submitCallbackSuccess)){
              this.mainDialogInfo.submitCallbackSuccess({resData: data})
            }
          })
          break;
        default:
          this.sksUtils.msgError("未知的变更标识");
          break;
      }
    },
  }
}
</script>

<style scoped>
</style>
